<template>
  <view class="seal">
    <view class="seal-img" v-if="sealInfo.sealImageId">
      <u-image
        width="100%"
        mode="widthFix"
        :src="sealInfo.sealImageId"
      ></u-image>
    </view>
    <view class="error seal-content" v-if="sealInfo.sealAuditStatus === 0">
      您的印章被驳回。<text v-if="sealInfo.sealRejectReason"
        >原因：{{ sealInfo.sealRejectReason }}</text
      >
    </view>
    <view class="error seal-content" v-if="sealInfo.sealAuditStatus === 2">
      印章审核需要1-2个工作日
    </view>
    <view class="error seal-content" v-if="sealInfo.sealAuditStatus === 3">
      您尚未设置印章，点击进行设置
    </view>

    <view class="footer">
      <u-button
        :custom-style="{
          background:
            'linear-gradient(90deg, #14b4fe 14%, rgb(1, 119, 253) 99%)',
        }"
        v-if="sealInfo.sealAuditStatus !== 2"
        type="primary"
        @click="settingSeal"
        :loading="sealLoading"
        shape="circle"
        >{{
          !sealInfo.manageAuthExpireFlag
            ? '授权设置个人印章'
            : sealInfo.sealAuditStatus === 3
            ? '设置个人印章'
            : '更换个人印章'
        }}</u-button
      >
    </view>
  </view>
</template>
<script lang="ts" setup>
import { useUserStoreWidthOut } from '@/stores/user';
import { checkAuthorize, getPersonalSeal } from '@/api/modules/auth';
import { onShow } from '@dcloudio/uni-app';

import { getEnvValue } from '@/utils/env';
const { VITE_PC_URL, VITE_BASE_URL } = getEnvValue<any>([
  'VITE_PC_URL',
  'VITE_BASE_URL',
]);

const sealLoading = ref(false);
const useUser = useUserStoreWidthOut();
const userInfo: any = useUser.getUserInfo;
const settingSeal = async () => {
  sealLoading.value = true;
  let res: any = await checkAuthorize({
    redirectUrl: 'wechat://back',
  });
  sealLoading.value = false;
  if (res.isAuthorized) {
    // 跳转至画印章页面
    // const href = `${getPCUrl()}/home/autograph?token=${useUser.getToken.substring(
    //   6
    // )}&isMobile=1`;
    const href = `${VITE_PC_URL}?token=${useUser.getToken.substring(
      6
    )}&isMobile=1`;
    uni.setStorageSync('signHref', href);
    (uni as any).$Router.push({
      name: 'autograph',
    });
  } else {
    // 使用了个人认证的相同逻辑，已自测
    uni.navigateTo({
      url:
        '/pages/my/sign/webview/index?url=' + encodeURIComponent(res.authUrl),
    });
  }
};

const sealInfo = ref<{
  sealImageId?: string; // 印章图片id
  sealAuditStatus?: number; // 印章审核结果 1通过 2待审核 0驳回 3未设置
  sealRejectReason?: string; // 驳回原因
  manageAuthExpireFlag?: boolean; // 授权是否过期
}>({});
// 获取印章
const getPersonalSealInfo = () => {
  getPersonalSeal({
    userId: userInfo.userId,
  }).then((res: any) => {
    sealInfo.value = res;
    // 判断如果没有上传过印章，手动给一个预设好的状态值
    if (!res.sealImageId) {
      sealInfo.value.sealAuditStatus = 3;
    }
    if (sealInfo.value.sealImageId) {
      sealInfo.value.sealImageId =
        VITE_BASE_URL +
        '/doc/Rest/doc/file/filePreview' +
        '?docId=' +
        sealInfo.value.sealImageId;
    }
  });
};
onShow(() => {
  getPersonalSealInfo();
});
</script>
<style lang="scss">
.seal {
  padding-top: 30rpx;
}
.seal-img {
  background-color: #fff;
  width: 90%;
  min-height: 300rpx;
  margin: 0 auto;
  border-radius: 30rpx;
}
.seal-content {
  margin-top: 20rpx;
  text-align: center;
}
.footer {
  width: 90%;
  margin: 30rpx auto 0;
}
.u-image {
  min-height: 300rpx;
}
</style>
